package MyOs;
import queue.*;
import MyOs.*;
public class ProcessManager {
    private PcbPool a;//建立一个初始Pcb池
    private static linkqueue c=new linkqueue();;//建立一个可供操作的Pcb队列
    public ProcessManager() {
          a=new PcbPool();
          //a.ShowPid();此行代码仅仅供测试使用
    }

    // 创建进程,PcbPool中的getOne函数带有异常判别处理，若PcbPool池为空，则会跳出异常语句
    public Pcb createProcess(String name, int time) {
        Pcb cp=a.getOne();
        a.returnOne();
        c.enQueue(cp.getPid());
        c.choose(cp.getPid()).setName(name);
        c.choose(cp.getPid()).setRemainTime(time);
        cp.setName(name);
        cp.setRemainTime(time);
        return cp;  
    }
    
    // 撤销进程
    
    public void destroyProcess(int pid) {
         c.choose(pid);
         c.delete(pid);
    }

    public void destroyAllProcess() {
           while(!c.isEmpty())
               c.deQueue();
    }

    // 设为就绪态
    public void ready(int pid) {
          Pcb cb=c.choose(pid);
          cb.setReady();
    }

    // 设为运行态
    public void running(int pid) {
         Pcb cb=c.choose(pid);
         cb.setRunning();
    }

    // 设为等待态
    public void waiting(int pid) {
         Pcb cb=c.choose(pid);
         cb.setWaiting();
    }
    
    public void showInfo(){
        if(c.isEmpty())
            throw new IllegalArgumentException("进程队列为空，无法显示进程信息");
        while(!c.isEmpty()){
            Pcb cb=c.deQueue();
            System.out.println(" "+cb.getPid()+" "+cb.getName()+" "+cb.getRemainTime()+" "+cb.getStatus());
        }
}
    public static linkqueue getOperationQueue(){//该函数用于完成和PcbQueue之间数据的交互
            return c;
    }
            
}  

